package com.rongshu.sendmessage.activity;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.adapter.MessageAdapter;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.bean.HealthyRegimenList;
import com.rongshu.sendmessage.util.AnimationUtil;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.DialogUtil;
import com.rongshu.sendmessage.util.ObjectUtil;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.httpUtil.Constant;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.httpUtil.IModel;
import com.rongshu.sendmessage.util.itemDecoration.MyItemDecoration;
import com.rongshu.sendmessage.view.refresh.ProgressLayout;
import com.rongshu.sendmessage.view.refresh.RefreshListenerAdapter;
import com.rongshu.sendmessage.view.refresh.TwinklingRefreshLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MoreMessageActivity extends BaseActivity {

    private RecyclerView recycleView;
    private SmartRefreshLayout list_refresh;
    private String pullType="";
    private int page=1;
    private List<HealthyRegimenList> mList;
    private MessageAdapter mAdapter;
    private String latitude,longitude;
    private String userId;
    private TextView tv_main_number;
    private TextView tv_stop_number;
    private RelativeLayout re_dingzhi_fabu;
    private RelativeLayout re_my_yunshu;
    private TextView tv_my_yunshu;
    private TextView tv_dingzhi_fabu;
    private ImageView iv_my_yunshu;
    private ImageView iv_dingzhi_fabu;
    private boolean isDingZhi;
    private String source;
    private View noresult_view;
    private View re_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_message);
        userId=MyApplication.mSettings.getString(HYContent.UserId,"");
        source = getIntent().getStringExtra("source");
        initView();
        pullType= Constant.PULLTYPE_DOWN;
        initData();
    }

    private void initView() {
        noresult_view=findViewById(R.id.noresult_view);
        TextView tv_no_text = findViewById(R.id.tv_no_text);
        tv_no_text.setText("这里空空如也");
        re_content=findViewById(R.id.re_content);
        TextView title = findViewById(R.id.title);
        View titleLayLeft = findViewById(R.id.titleLayLeft);
        titleLayLeft.setOnClickListener(view->finish());
        title.setText(getResources().getString(R.string.app_name));
        recycleView = findViewById(R.id.recycleView);
        recycleView.setLayoutAnimation(AnimationUtil.getAnimationController());
//        listView.setEmptyView(emptyView);
        list_refresh = findViewById(R.id.list_refresh);
        list_refresh.setEnableLoadmore(true);
        list_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pullType= Constant.PULLTYPE_DOWN;
                page=1;
                initData();

            }
        });
        list_refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pullType = Constant.PULLTYPE_UP;
                page++;
                initData();
            }
        });
        mList=new ArrayList<>();
        recycleView.setHasFixedSize(true);//设置固定大小
        recycleView.setItemAnimator(new DefaultItemAnimator());//设置默认动画
        recycleView.addItemDecoration(new MyItemDecoration());
        recycleView.addItemDecoration(new MyItemDecoration(0, 0));
        GridLayoutManager mLayoutManage = new GridLayoutManager(mContext, 1);
        recycleView.setLayoutManager(mLayoutManage);
        mAdapter = new MessageAdapter(mContext,"more", R.layout.message_item, mList);
        recycleView.setAdapter(mAdapter);
        //recycleview的点击事件
        mAdapter.setOnItemClickListener((view, position) -> {
            if (view.getId()==R.id.ll_bianji){
                //去编辑

            }else{
                Intent intent=new Intent(mContext, MessageDetailActivity.class);
                String id = mList.get(position).getId();
                intent.putExtra("articleId",id);
                startActivity(intent);
            }
        });
        re_dingzhi_fabu = findViewById(R.id.re_over_fabu);
        re_my_yunshu = findViewById(R.id.re_in_yunshu);
        tv_my_yunshu = findViewById(R.id.tv_in_yunshu);
        tv_dingzhi_fabu = findViewById(R.id.tv_over_fabu);
        iv_my_yunshu = findViewById(R.id.iv_in_yunshu);
        iv_dingzhi_fabu = findViewById(R.id.iv_over_fabu);
        re_dingzhi_fabu.setOnClickListener(view->{
            isDingZhi =true;
            SetHeadSearch();
        });
        re_my_yunshu.setOnClickListener(view->{
            isDingZhi =false;
            SetHeadSearch();
        });
        if ("my".equals(source)){
            isDingZhi =false;
        }else{
            isDingZhi =true;
        }
        SetHeadSearch();
    }
    private void SetHeadSearch(){
        if (isDingZhi){
            tv_dingzhi_fabu.setTextColor(getResources().getColor(R.color.main_color_new_1));
            tv_my_yunshu.setTextColor(Color.parseColor("#4C4C4C"));
            tv_my_yunshu.setTextSize(12);
            tv_dingzhi_fabu.setTextSize(14);
            iv_dingzhi_fabu.setVisibility(View.VISIBLE);
            iv_my_yunshu.setVisibility(View.INVISIBLE);
        }else{
            tv_dingzhi_fabu.setTextColor(Color.parseColor("#4C4C4C"));
            tv_dingzhi_fabu.setTextSize(12);
            tv_my_yunshu.setTextColor(getResources().getColor(R.color.main_color_new_1));
            tv_my_yunshu.setTextSize(14);
            iv_dingzhi_fabu.setVisibility(View.INVISIBLE);
            iv_my_yunshu.setVisibility(View.VISIBLE);
        }
        page=1;
        pullType = Constant.PULLTYPE_DOWN;
        initData();
    }
    private void initData() {
        DialogUtil.showProgress(mContext);
        Map<String, String> param = new HashMap<String, String>();
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        param.put("keyWord", "");
        param.put("toppingIsNo", "");//isDingZhi +
        param.put("page", page+"");
        //
        OkGo.<String>post(new HYContent().GET_FABU_LIST_URL).params(param).tag("GET_FABU_LIST_URL").
                execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtil.hideProgress();
                        if (pullType.equals(Constant.PULLTYPE_DOWN)) {
                            list_refresh.finishRefresh(true);
                            mList.clear();
                        } else {
                            list_refresh.finishLoadmore(true);
                        }
                        try {
                            IModel.AjaxResult jr = new IModel.AjaxResult(response.body());
                            final int code = jr.getCode();
                            if (code>0){
                                List<HealthyRegimenList> messageLists = JSON.parseArray(JSON.toJSONString(jr.getJSONArrayData()), HealthyRegimenList.class);

                                mList.addAll(messageLists);
                                if (mList.size()!=0&&messageLists.size()==0){
                                    ToastUtils.show(mContext,"没有更多了");
                                }
                                mAdapter.setmList(mList);
                                mAdapter.notifyDataSetChanged();
                                if (mList.size()==0){
                                    noresult_view.setVisibility(View.VISIBLE);
                                    recycleView.setVisibility(View.GONE);
                                }else{
                                    noresult_view.setVisibility(View.GONE);
                                    recycleView.setVisibility(View.VISIBLE);

                                }
                            }else{
                                ToastUtils.show(mContext,jr.getMsg());
                                noresult_view.setVisibility(View.VISIBLE);
                                recycleView.setVisibility(View.GONE);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            noresult_view.setVisibility(View.VISIBLE);
                            recycleView.setVisibility(View.GONE);
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtil.hideProgress();
                        noresult_view.setVisibility(View.VISIBLE);
                        recycleView.setVisibility(View.GONE);
                        if (pullType.equals(Constant.PULLTYPE_DOWN)) {
                            list_refresh.finishRefresh(false);
                        } else {
                            list_refresh.finishLoadmore(false);
                        }
                    }
                });
    }

}